package com.example.exam.service.impl;

import com.example.exam.dao.ChoiceDao;
import com.example.exam.entity.Choice;
import com.example.exam.service.ChoiceService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Choice)表服务实现类
 *
 * @author makejava
 * @since 2021-09-04 15:02:44
 */
@Service("choiceService")
public class ChoiceServiceImpl implements ChoiceService {
    @Resource
    private ChoiceDao choiceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param chid 主键
     * @return 实例对象
     */
    @Override
    public Choice queryById(Integer chid) {
        return this.choiceDao.queryById(chid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Choice> queryAllByLimit(int offset, int limit, String subject, String question) {
        PageHelper.startPage(offset, limit);
        return this.choiceDao.queryAll3(subject, question);
    }

    /**
     * 新增数据
     *
     * @param choice 实例对象
     * @return 实例对象
     */
    @Override
    public Choice insert(Choice choice) {
        this.choiceDao.insert(choice);
        return choice;
    }

    /**
     * 修改数据
     *
     * @param choice 实例对象
     * @return 实例对象
     */
    @Override
    public Choice update(Choice choice) {
        this.choiceDao.update(choice);
        return this.queryById(choice.getChid());
    }

    /**
     * 通过主键删除数据
     *
     * @param chid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer chid) {
        return this.choiceDao.deleteById(chid) > 0;
    }

    @Override
    public List<Choice> queryAll() {
        return choiceDao.queryAll2();
    }

    @Override
    public List<Choice> queryBySubject(String subject) {
        return this.choiceDao.queryBySubject(subject);
    }

    @Override
    public List<Choice> queryByChLike(String chquestion) {
        return this.choiceDao.queryByChLike(chquestion);
    }
}